package com.suray.wcs.web.system.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.system.entity.PortTaskConfigDB;
import com.suray.wcs.service.system.service.impl.PortTaskConfigDBServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/portConfig")
public class PortConfigController extends BaseController {
    @Autowired
    private PortTaskConfigDBServiceImpl portTaskConfigDBService;
    /**
     * 获取配置
     *
     * @throws Exception
     */
    @GetMapping(value = "/getPortConfigList")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('portConfig:query')")
    @ApiOperation("查询库口配置")
    public TableDataInfo getAssignConfigList(PortTaskConfigDB portTaskConfigDB) {
        startPage();
        List<PortTaskConfigDB> portTaskConfigDBList = portTaskConfigDBService.selectList(portTaskConfigDB);
        return getDataTable(portTaskConfigDBList);
    }
    /**
     * 新增配置
     *
     * @throws Exception
     */
    @PostMapping(value = "/addPortConfig")
    @PreAuthorize("@ss.hasPermi('portConfig:add')")
    @Log(title = "库口配置", method = "新增库口配置", businessType = BusinessType.INSERT)
    @ResponseBody
    @ApiOperation("新增库口配置")
    public ResponseData addPortConfig(@RequestBody PortTaskConfigDB portTaskConfigDB) {
        if (portTaskConfigDB.getLayer() == null) {
            return ResponseData.error("层不存在");
        }
        if (portTaskConfigDB.getPortId() == null) {
            return ResponseData.error("库口不存在");
        }
        PortTaskConfigDB portTaskConfigDB1=  portTaskConfigDBService.getPortConfigByFloorAndPortId(portTaskConfigDB.getLayer(),portTaskConfigDB.getPortId());
        if(portTaskConfigDB1!=null){
            return ResponseData.error("插入失败,该层已存在库口配置");
        }
        if (portTaskConfigDB.getOutTaskNum() == null) {
            portTaskConfigDB.setOutTaskNum(0);
        }
        boolean flag = portTaskConfigDBService.insertPortConfig(portTaskConfigDB);
        if (flag) {
            return ResponseData.success("插入成功");
        } else {
            return ResponseData.error("插入失败");
        }
    }

    /**
     * 更新配置
     *
     * @throws Exception
     */
    @PostMapping(value = "/updatePortConfig")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('portConfig:edit')")
    @Log(title = "库口配置", method = "修改库口配置", businessType = BusinessType.UPDATE)
    @ApiOperation("修改库口配置")
    public ResponseData updatePortConfig(@RequestBody PortTaskConfigDB portTaskConfigDB) {
        if (portTaskConfigDB.getLayer() == null) {
            return ResponseData.error("层不存在");
        }
        if (portTaskConfigDB.getPortId() == null) {
            return ResponseData.error("库口不存在");
        }
        boolean flag = portTaskConfigDBService.updatePortConfig(portTaskConfigDB);
        if (flag) {
            return ResponseData.success("修改成功");
        } else {
            return ResponseData.error("修改失败");
        }
    }
    /**
     * 获取配置
     *
     * @throws Exception
     */
    @GetMapping(value = "/getPortConfigInfo/{id}")
    @ResponseBody
    @ApiOperation("查询库口配置")
    public ResponseData getPortConfigInfo(@PathVariable Integer id) {
        PortTaskConfigDB portTaskConfigDB1=  portTaskConfigDBService.findConfigById(id);
        return ResponseData.success("查询完成", portTaskConfigDB1);
    }

    /**
     * 删除配置
     *
     * @throws Exception
     */
    @DeleteMapping(value = "/deletePortConfig/{ids}")
    @PreAuthorize("@ss.hasPermi('portConfig:remove')")
    @Log(title = "库口配置", method = "删除库口配置", businessType = BusinessType.DELETE)
    @ResponseBody
    @ApiOperation("删除库口配置")
    public ResponseData deleteAssignConfig(@PathVariable List<Integer> ids) {

        boolean flag = portTaskConfigDBService.deletePortConfig(ids);
        if (flag) {
            return ResponseData.success("删除成功");
        } else {
            return ResponseData.error("删除失败");
        }
    }
}
